<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\bookpack;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\bookpack\BookpackAssociation;
use app\common\model\book\Book;
use app\common\lists\ListsSearchInterface;
use app\common\service\FileService;


/**
 * BookpackAssociation列表
 * Class BookpackAssociationLists
 * @package app\adminapi\lists
 */
class BookpackAssociationLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/01/11 19:06
     */
    public function setSearch(): array
    {
        return [
            '%like%' => ['b.book_name'],
            '=' => ['book_id', 'bookpack_id', 'sort'],
        ];
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/01/11 19:06
     */
    public function lists(): array
    {
        $list = BookpackAssociation::alias('ba')
            ->join('book b', 'b.id = ba.book_id')->join('category c', 'b.sort_id = c.id')
            ->where($this->searchWhere)->where($this->queryWhere())
            ->field(['ba.id', 'ba.book_id', 'ba.bookpack_id', 'ba.sort', 'b.book_name', 'b.thumb', 'b.channel_type', 'b.book_type', 'b.intro', 'b.sort_id','c.name as category_name'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['sort' => 'asc'])
            ->select()
            ->toArray();
        foreach ($list as &$item){
            $item['thumb'] = FileService::getFileUrl($item['thumb']);
            $item['tooltipSwitch'] = false;
        }
        return $list;
    }

    /**
     * @notes 搜索条件
     * @author 段誉
     * @date 2023/2/24 16:08
     */
    public function queryWhere()
    {
        $where = [];
        if (!empty($this->params['sort_id'])){
            $where[] = ['b.sort_id','=',$this->params['sort_id']];
        }
        if (!empty($this->params['channel_type'])){
            $where[] = ['b.channel_type','=',$this->params['channel_type']];
        }

        return $where;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/01/11 19:06
     */
    public function count(): int
    {
        return BookpackAssociation::alias('ba')->join('book b', 'b.id = ba.book_id')->join('category c', 'b.sort_id = c.id')->where($this->searchWhere)->where($this->queryWhere())->count();
    }

}